<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends HomeController {

	/**
	 * 初始化
	 */
	public function __construct() {
		parent::__construct();
	}

	public function indextest() {
	}
	
	/**
	 * 通许录
	 */
	public function indexp() {
		$key  = I('get.searchkey','','strip_tags');
		$nickname = I('get.nickname','','strip_tags');
		if(!empty($nickname)) {
			$wheren['nickname'] = array('like','%'.$key.'%');
		}
		$address = I('get.address','','strip_tags');
		if(!empty($address)) {
			$wheren['address'] = array('like','%'.$key.'%');
		}
		$mobile = I('get.mobile','','strip_tags');
		if(!empty($mobile)) {
			$wheren['mobile'] = array('like','%'.$key.'%');
		}
		$qq = I('get.qq','','strip_tags');
		if(!empty($qq)) {
			$wheren['qq'] = array('like','%'.$key.'%');
		}
		$cs = I('get.cs','','strip_tags');
		if(!empty($cs)) {
			$wheren['cs'] = array('like','%'.$key.'%');
		}
		$qg = I('get.qg','','strip_tags');
		if(!empty($qg)) {
			$wheren['qg'] = array('like','%'.$key.'%');
		}
		$company = I('get.company','','strip_tags');
		if(!empty($company)) {
			$wheren['company'] = array('like','%'.$key.'%');
		}

		//$province = I('get.province','','strip_tags');
		//if(!empty($province) && $province != '全部') {
			//$wheren['province'] = array('like','%'.$key.'%');
		//} else {
			//unset($_SESSION['province']);
		//}

		//$city = I('get.city','','strip_tags');
		//if(!empty($city)) {
			//$wheren['city'] = array('like','%'.$key.'%');
		//}

		if(empty($nickname) && empty($address) && empty($mobile) && empty($qq) && empty($cs) && empty($qg) && empty($company)) {
			if(!empty($key)) {
			$wheren['nickname'] = array('like','%'.$key.'%');
			}
		} else {
			if(!empty($wheren)) {
			$wheren['_logic'] = 'or';
			}
		}

		$getprovince = I('get.province','','strip_tags');
		if(!empty($getprovince)) {
			if($getprovince == '全部') {
				//$_SESSION['province'] = '';
				unset($_SESSION['province']);
			} else {
				if($getprovince == 96) {
					die();
				}
				$_SESSION['province'] = $getprovince;
			}
		}

		if(!empty($wheren)) {
			$where['_complex'] = $wheren;
		}
		if(!empty($_SESSION['province'])) {
			$where['province'] = $_SESSION['province'];
		}

		$field = 'id,nickname,headimgurl,ping';
		$list = D('Admin/address')->hselect($where,'',$field);
		//$count = D('address')->hselectcount($where,'',$field);
		$this->assign('list',$list);
		$this->assign('listzm',$listzm);
		$this->assign('total',count($list));

		/**jssdk***/
		$signPackage = $this->signPackage;
		$signPackage['title'] = "苗木通讯录";
		$signPackage['link'] = 'http://'.C('DOMAIN').U('Index/index');
		$signPackage['desc'] = "众里寻他千百度，苗友尽在通讯录，苗商驰骋天地间，买卖沟通更方便。";
		$signPackage['imgUrl'] = 'http://zhumeng777.com/Uploads/2016-09-18/57de84aca6a35.jpg';
		$this->assign('signPackage',$signPackage);
		/****/
		$banner = M('banner')->select();
		$this->assign('banner',$banner);

		$this->display();
	}

	public function ajax_indexp() {
		$key  = I('get.searchkey','','strip_tags');
		$nickname = I('get.nickname','','strip_tags');
		if(!empty($nickname)) {
			$wheren['nickname'] = array('like','%'.$key.'%');
		}
		$address = I('get.address','','strip_tags');
		if(!empty($address)) {
			$wheren['address'] = array('like','%'.$key.'%');
		}
		$mobile = I('get.mobile','','strip_tags');
		if(!empty($mobile)) {
			$wheren['mobile'] = array('like','%'.$key.'%');
		}
		$qq = I('get.qq','','strip_tags');
		if(!empty($qq)) {
			$wheren['qq'] = array('like','%'.$key.'%');
		}
		$cs = I('get.cs','','strip_tags');
		if(!empty($cs)) {
			$wheren['cs'] = array('like','%'.$key.'%');
		}
		$qg = I('get.qg','','strip_tags');
		if(!empty($qg)) {
			$wheren['qg'] = array('like','%'.$key.'%');
		}
		$company = I('get.company','','strip_tags');
		if(!empty($company)) {
			$wheren['company'] = array('like','%'.$key.'%');
		}
		
		if(empty($nickname) && empty($address) && empty($mobile) && empty($qq) && empty($cs) && empty($qg) && empty($company)) {
			if(!empty($key)) {
			$wheren['nickname'] = array('like','%'.$key.'%');
			}
		} else {
			if(!empty($wheren)) {
			$wheren['_logic'] = 'or';
			}
		}

		$getprovince = I('get.province','','strip_tags');
		if(!empty($getprovince)) {
			if($getprovince == '全部') {
				//$_SESSION['province'] = '';
				unset($_SESSION['province']);
			} else {
				if($getprovince == 96) {
					die();
				}
				$_SESSION['province'] = $getprovince;
			}
		}

		if(!empty($wheren)) {
			$where['_complex'] = $wheren;
		}
		if(!empty($_SESSION['province'])) {
			$where['province'] = $_SESSION['province'];
		}


		$field = 'id,nickname,nums,headimgurl';
		/****/
		$page = intval($_GET['p']) < 1?1:intval($_GET['p']);
		$page++;
		$time = time();
	    $pretime = $time-(60*60*24*365);
		$where['paytime'] = ['gt',$pretime];
		
		$order = 'ping asc , id desc';
		$address = M('address'); 
		$list = $address->field($field)->where($where)->order($order)->page($page.',15')->select();
		foreach($list as $key=>$value) {
			$list[$key]['nickname'] = mbsubstr(delzm($value['nickname']),0,50);
			$list[$key]['headimgurl'] = headimgsize($value['headimgurl'],96);
		}
		echo json_encode($list);
		/****/

	}

	public function test() {
		$list = D('address')->hselect($where,'',$field);
		echo D('address')->getLastsql();
		//print_r($list);
	}

	public function index() {
		//print_r($_SESSION);
		//die();
		$key  = I('get.searchkey','','strip_tags');
		$nickname = I('get.nickname','','strip_tags');
		if(!empty($nickname)) {
			$wheren['nickname'] = array('like','%'.$key.'%');
		}
		$address = I('get.address','','strip_tags');
		if(!empty($address)) {
			$wheren['address'] = array('like','%'.$key.'%');
		}
		$mobile = I('get.mobile','','strip_tags');
		if(!empty($mobile)) {
			$wheren['mobile'] = array('like','%'.$key.'%');
		}
		$qq = I('get.qq','','strip_tags');
		if(!empty($qq)) {
			$wheren['qq'] = array('like','%'.$key.'%');
		}
		$cs = I('get.cs','','strip_tags');
		if(!empty($cs)) {
			$wheren['cs'] = array('like','%'.$key.'%');
		}
		$qg = I('get.qg','','strip_tags');
		if(!empty($qg)) {
			$wheren['qg'] = array('like','%'.$key.'%');
		}
		$company = I('get.company','','strip_tags');
		if(!empty($company)) {
			$wheren['company'] = array('like','%'.$key.'%');
		}

		//$province = I('get.province','','strip_tags');
		//if(!empty($province) && $province != '全部') {
			//$wheren['province'] = array('like','%'.$key.'%');
		//} else {
			//unset($_SESSION['province']);
		//}

		//$city = I('get.city','','strip_tags');
		//if(!empty($city)) {
			//$wheren['city'] = array('like','%'.$key.'%');
		//}
		
		if(empty($nickname) && empty($address) && empty($mobile) && empty($qq) && empty($cs) && empty($qg) && empty($company)) {
			if(!empty($key)) {
			$wheren['nickname'] = array('like','%'.$key.'%');
			}
		} else {
			if(!empty($wheren)) {
			$wheren['_logic'] = 'or';
			}
		}

		$getprovince = I('get.province','','strip_tags');
		if(!empty($getprovince)) {
			if($getprovince == '全部') {
				//$_SESSION['province'] = '';
				unset($_SESSION['province']);
			} else {
				if($getprovince == 96) {
					die();
				}
				$_SESSION['province'] = $getprovince;
			}
		}

		if(!empty($wheren)) {
			$where['_complex'] = $wheren;
		}
		if(!empty($_SESSION['province'])) {
			$where['province'] = $_SESSION['province'];
		}

		//$where['id'] = array('NEQ',$this->userinfo['id']);
		$field = 'id,nickname,nums,headimgurl';
		$list = D('address')->hselect($where,'',$field,'0,15');

		$count = D('address')->hselectcount($where,'',$field);
		$this->assign('list',$list);
		$this->assign('listzm',$listzm);
		$this->assign('total',$count);

		/**jssdk***/
		$signPackage = $this->signPackage;
		$signPackage['title'] = "苗木通讯录";
		$signPackage['link'] = 'http://'.C('DOMAIN').U('Index/index');
		$signPackage['desc'] = "众里寻他千百度，苗友尽在通讯录，苗商驰骋天地间，买卖沟通更方便。";
		$signPackage['imgUrl'] = 'http://zhumeng777.com/Uploads/2016-09-18/57de84aca6a35.jpg';
		$this->assign('signPackage',$signPackage);
		/****/

		$url =  U('Index/ajax_index',$_GET);
		$urls = str_replace('.html','',$url);
		$this->assign('url',$urls);

		$banners = M('banner')->select();
		$this->assign('banners',$banners);
		$this->display();
	}

	public function ajax_index() {
		$key  = I('get.searchkey','','strip_tags');
		$nickname = I('get.nickname','','strip_tags');
		if(!empty($nickname)) {
			$wheren['nickname'] = array('like','%'.$key.'%');
		}
		$address = I('get.address','','strip_tags');
		if(!empty($address)) {
			$wheren['address'] = array('like','%'.$key.'%');
		}
		$mobile = I('get.mobile','','strip_tags');
		if(!empty($mobile)) {
			$wheren['mobile'] = array('like','%'.$key.'%');
		}
		$qq = I('get.qq','','strip_tags');
		if(!empty($qq)) {
			$wheren['qq'] = array('like','%'.$key.'%');
		}
		$cs = I('get.cs','','strip_tags');
		if(!empty($cs)) {
			$wheren['cs'] = array('like','%'.$key.'%');
		}
		$qg = I('get.qg','','strip_tags');
		if(!empty($qg)) {
			$wheren['qg'] = array('like','%'.$key.'%');
		}
		$company = I('get.company','','strip_tags');
		if(!empty($company)) {
			$wheren['company'] = array('like','%'.$key.'%');
		}
		
		if(empty($nickname) && empty($address) && empty($mobile) && empty($qq) && empty($cs) && empty($qg) && empty($company)) {
			if(!empty($key)) {
			$wheren['nickname'] = array('like','%'.$key.'%');
			}
		} else {
			if(!empty($wheren)) {
			$wheren['_logic'] = 'or';
			}
		}

		$getprovince = I('get.province','','strip_tags');
		if(!empty($getprovince)) {
			if($getprovince == '全部') {
				//$_SESSION['province'] = '';
				unset($_SESSION['province']);
			} else {
				if($getprovince == 96) {
					die();
				}
				$_SESSION['province'] = $getprovince;
			}
		}

		if(!empty($wheren)) {
			$where['_complex'] = $wheren;
		}
		if(!empty($_SESSION['province'])) {
			$where['province'] = $_SESSION['province'];
		}


		$field = 'id,nickname,nums,headimgurl';
		/****/
		$page = intval($_GET['p']) < 1?1:intval($_GET['p']);
		$page++;
		$time = time();
	    $pretime = $time-(60*60*24*365);
		$where['paytime'] = ['gt',$pretime];
		
		$order = 'nums desc , updatetime desc, id asc';
		$address = M('address'); 
		$list = $address->field($field)->where($where)->order($order)->page($page.',15')->select();
		foreach($list as $key=>$value) {
			$list[$key]['nickname'] = mbsubstr(delzm($value['nickname']),0,13);
			$list[$key]['headimgurl'] = headimgsize($value['headimgurl'],96);
		}
		echo json_encode($list);
		/****/

	}
	
	/**
	 * 添加常用联系人
	 */
	public function addcommon() {
		$id = I('post.id','','intval');
		if(!empty($id)) {
			$idarr = array();
			$common = $this->userinfo['mans'];
			if(!empty($common)) {
				$idarr = array_filter(explode(',',$common));
			}
			$idarr[] = $id;
			$data['mans'] = implode(',',$idarr);
			$msg = D('address')->where(array('id'=>$this->userinfo['id']))->save($data);
			if(!empty($msg)) {
				echo 1;
			} else {
				echo 2;
			}
		} else {
			echo 3;
		}
	}

	/**
	 * ajax 取消常用联系人
	 */
	public function cancelcommon() {
		$id = I('post.id','','intval');
		if(!empty($id)) {
			$idarr = array();
			$common = $this->userinfo['mans'];
			if(!empty($common)) {
				$idarr = array_filter(explode(',',$common));
			}
			foreach($idarr as $key=>$value) {
				if($value == $id) {
					unset($idarr[$key]);
				}
			}
			$data['mans'] = implode(',',$idarr);
			$msg = D('address')->where(array('id'=>$this->userinfo['id']))->save($data);
			if(!empty($msg)) {
				echo 1;
			} else {
				echo 2;
			}
		} else {
			echo 3;
		}
	}

	/**
	 * 常用联系人
	 */
	public function common() {
		if(!empty($this->userinfo['mans'])) {
			$where['id'] = array('in',$this->userinfo['mans']);

			$getprovince = I('get.province','','strip_tags');
			if(!empty($getprovince)) {
				if($getprovince == '全部') {
					$_SESSION['province'] = '';
				} else {
					$_SESSION['province'] = $getprovince;
				}
			}
			if(!empty($_SESSION['province'])) {
				$where['province'] = $_SESSION['province'];
			}

			$field = 'id,nickname,headimgurl,ping';
			$list = D('Admin/address')->hselect($where,'',$field);
			$this->assign('list',$list);
		}
		/**jssdk***/
		$signPackage = $this->signPackage;
		$signPackage['title'] = "苗木通讯录";
		$signPackage['link'] = 'http://'.C('DOMAIN').U('Index/index');
		$signPackage['desc'] = "众里寻他千百度，苗友尽在通讯录，苗商驰骋天地间，买卖沟通更方便。";
		$signPackage['imgUrl'] = 'http://zhumeng777.com/Uploads/2016-09-18/57de84aca6a35.jpg';
		$this->assign('signPackage',$signPackage);
		/****/
		$this->display();
	}

	/**
	 * 最新出售信息	
	 */
	public function newscs() {
		//$where['cs']=array('EXP','IS NOT NULL');
		$where['cs']=array(array('EXP','IS NOT NULL'),array('EXP','<> ""'));
		//$where['id'] = array('NEQ',$this->userinfo['id']);
		$getprovince = I('get.province','','strip_tags');
		if(!empty($getprovince)) {
			if($getprovince == '全部') {
				$_SESSION['province'] = '';
			} else {
				$_SESSION['province'] = $getprovince;
			}
		}
		if(!empty($_SESSION['province'])) {
			$where['province'] = $_SESSION['province'];
		}

		$time = time();
		$start = $time-(30*24*60*60);
		$where['updatetime'] = array('between',array($start,$time));
		$field = 'id,headimgurl,cs';
		$list = D('Admin/address')->hselect($where,'',$field);
		$this->assign('list',$list);

		/**jssdk***/
		$signPackage = $this->signPackage;
		$signPackage['title'] = "来自苗木通讯录微信公众平台的最新出售信息";
		$signPackage['link'] = 'http://'.C('DOMAIN').U('Index/newscs');
		$signPackage['desc'] = "众里寻他千百度，苗友尽在通讯录，苗商驰骋天地间，买卖沟通更方便。";
		$signPackage['imgUrl'] = 'http://zhumeng777.com/Uploads/2016-09-18/57de84aca6a35.jpg';
		$this->assign('signPackage',$signPackage);
		/****/

		$this->display();
	}

	public function newscstest() {
		//$where['cs']=array('EXP','IS NOT NULL');
		$where['cs']=array(array('EXP','IS NOT NULL'),array('EXP','<> ""'));
		//$where['id'] = array('NEQ',$this->userinfo['id']);
		$getprovince = I('get.province','','strip_tags');
		if(!empty($getprovince)) {
			if($getprovince == '全部') {
				$_SESSION['province'] = '';
			} else {
				$_SESSION['province'] = $getprovince;
			}
		}
		if(!empty($_SESSION['province'])) {
			$where['province'] = $_SESSION['province'];
		}

		$time = time();
		$start = $time-(30*24*60*60);
		$where['updatetime'] = array('between',array($start,$time));
		$list = D('Admin/address')->hselect($where,$order);
		$this->assign('list',$list);

		/**jssdk***/
		$signPackage = $this->signPackage;
		$signPackage['title'] = "来自苗木通讯录微信公众平台的最新出售信息";
		$signPackage['link'] = 'http://'.C('DOMAIN').U('Index/newscs');
		$signPackage['desc'] = "众里寻他千百度，苗友尽在通讯录，苗商驰骋天地间，买卖沟通更方便。";
		$signPackage['imgUrl'] = 'http://zhumeng777.com/Uploads/2016-09-18/57de84aca6a35.jpg';
		$this->assign('signPackage',$signPackage);
		/****/

		$this->display();
	}

	/**
	 * 最新求够
	 */
	public function newsqg() {
		$where['qg']=array(array('EXP','IS NOT NULL'),array('EXP','<> ""'));
		//$where['id'] = array('NEQ',$this->userinfo['id']);
		$getprovince = I('get.province','','strip_tags');
		if(!empty($getprovince)) {
			if($getprovince == '全部') {
				$_SESSION['province'] = '';
			} else {
				$_SESSION['province'] = $getprovince;
			}
		}
		if(!empty($_SESSION['province'])) {
			$where['province'] = $_SESSION['province'];
		}

		$time = time();
		$start = $time-(30*24*60*60);
		$where['updatetime'] = array('between',array($start,$time));
		$field = 'id,headimgurl,qg';
		$list = D('Admin/address')->hselect($where,'',$field);
		$this->assign('list',$list);
		/**jssdk***/
		$signPackage = $this->signPackage;
		$signPackage['title'] = "来自苗木通讯录微信公众平台的最新求购信息";
		$signPackage['link'] = 'http://'.C('DOMAIN').U('Index/newsqg');
		$signPackage['desc'] = "众里寻他千百度，苗友尽在通讯录，苗商驰骋天地间，买卖沟通更方便。";
		$signPackage['imgUrl'] = 'http://zhumeng777.com/Uploads/2016-09-18/57de84aca6a35.jpg';
		$this->assign('signPackage',$signPackage);
		/****/
		$this->display();
	}
	
	/**
	 * 最新加入企业
	 */
	public function newsaddress() {
		$getprovince = I('get.province','','strip_tags');
		if(!empty($getprovince)) {
			if($getprovince == '全部') {
				$_SESSION['province'] = '';
			} else {
				$_SESSION['province'] = $getprovince;
			}
		}
		if(!empty($_SESSION['province'])) {
			$where['province'] = $_SESSION['province'];
		}
		//$where['id'] = array('NEQ',$this->userinfo['id']);
		$field = 'id,nickname,headimgurl,ping';
		$list = D('Admin/address')->hselectnow($where,$order,$field);
		$this->assign('list',$list);
		/**jssdk***/
		$signPackage = $this->signPackage;
		$signPackage['title'] = "苗木通讯录";
		$signPackage['link'] = 'http://'.C('DOMAIN').U('Index/index');
		$signPackage['desc'] = "众里寻他千百度，苗友尽在通讯录，苗商驰骋天地间，买卖沟通更方便。";
		$signPackage['imgUrl'] = 'http://zhumeng777.com/Uploads/2016-09-18/57de84aca6a35.jpg';
		$this->assign('signPackage',$signPackage);
		/****/
		$this->display();
	}

	/**
	 * 通讯录详情
	 */
	public function info() {
		$id = I('get.id','','intval');
		if($this->userinfo['id']) {
			if($id == $this->userinfo['id']) {
				header('Location:'.U('Index/myself'));
				die();
			}
		}
		$info = D('address')->find($id);

		/**记录点击量**/
		$dwhere['dopenid'] = $info['openid'];
		$dwhere['dotheropenid'] = $_SESSION['info']['openid'];
		$num = D('num')->where($dwhere)->find();
		if(empty($num)) {
			$data['dopenid'] = $info['openid'];
			$data['dotheropenid'] = $_SESSION['info']['openid'];
			$data['dtime'] = time();
			$msg = D('num')->add($data);

			$inc = D('address')->where(array('id'=>$id))->setInc('nums');
		} else {
			if(date('Y-m-d') != date('Y-m-d',$num['dtime'])) {
				$data['dopenid'] = $info['openid'];
				$data['dotheropenid'] = $_SESSION['info']['openid'];
				$data['dtime'] = time();
				$wheres['did'] = $num['did'];
				$msg = D('num')->where($wheres)->save($data);
				$inc = D('address')->where(array('id'=>$id))->setInc('nums');
			}
		}
		/**记录点击量**/

		/**jssdk***/
		$signPackage = $this->signPackage;
		$signPackage['title'] = "苗木通讯录：".$info['nickname'];
		$signPackage['link'] = 'http://'.C('DOMAIN').U('Index/info',array('id'=>$info['id']));
		$signPackage['desc'] = "众里寻他千百度，苗友尽在通讯录，苗商驰骋天地间，买卖沟通更方便。";
		$signPackage['imgUrl'] = $info['headimgurl'];
		$this->assign('signPackage',$signPackage);
		/****/

		if(!empty($info['mobile'])) {
			$mobilearr = json_decode($info['mobile'],true);
			$info['mobilearr'] = $mobilearr;
			$this->assign('mobilearr',$mobilearr);
		}

		if(!empty($info['photo'])) {
			$info['photos'] = explode('|',$info['photo']);
		}
		$this->assign('photoarr',$info['photos']);

		$this->assign('info',$info);
		$mans = explode(',',$this->userinfo['mans']);
		$this->assign('mans',$mans);

		if(!empty($info['desc'])) {
			$descarr = json_decode($info['desc'],true);
			$this->assign('descarr',$descarr);
		}

		$this->display();
	}

	/**
	 * 个人中心
	 */
	public function myself() {
		$id = I('get.id','','intval');
		if(!empty($id)) {
			if($id != $this->userinfo['id']) {
				header('Location:'.U('Index/info',array('id'=>$id)));
				die();
			}
		}
		$manay = 30;
		$this->assign('manay',$manay);
		/*
		//$year = date('Y');
		$year = 2016;
		$string = $year."0101";
		$yeartime = strtotime($string);
		$this->assign('yeartime',$yeartime);

		$string = ($year+2)."0101";
		$nextyeartime = strtotime($string);
		$this->assign('nextyeartime',$nextyeartime);
		 */

		$info = D('address')->find($this->userinfo['id']);

		$time = time();
		$pretime = $time-(60*60*24*365);
		$this->assign('pretime',$pretime);

		if(!empty($info['paytime'])) {
			$txtime = $pretime+(60*60*24*30*10);
			$this->assign('txtime',$txtime);

			$dqtime = $info['paytime']+(60*60*24*365);
			$this->assign('dqtime',$dqtime);
		}

		/**jssdk***/
		$signPackage = $this->signPackage;
		$signPackage['title'] = "苗木通讯录：".$info['nickname'];
		$signPackage['link'] = 'http://'.C('DOMAIN').U('Index/info',array('id'=>$info['id']));
		$signPackage['desc'] = "众里寻他千百度，苗友尽在通讯录，苗商驰骋天地间，买卖沟通更方便。";
		$signPackage['imgUrl'] = $info['headimgurl'];
		$this->assign('signPackage',$signPackage);
		/****/

		if(!empty($info['mobile'])) {
			$mobilearr = json_decode($info['mobile'],true);
			$info['mobilearr'] = $mobilearr;
			$this->assign('mobilearr',$mobilearr);
		}

		if(!empty($info['photo'])) {
			$info['photos'] = explode('|',$info['photo']);
		}
		$this->assign('photoarr',$info['photos']);

		if(!empty($this->userinfo['desc'])) {
			$descarr = json_decode($this->userinfo['desc'],true);
			$this->assign('descarr',$descarr);
		}

		$this->assign('info',$info);
		$this->display();
	}
	
	/**
	 * 图片裁剪 ajax 
	 */
	public function ajaxheadimg() {
		$base64 = I('post.str');
		if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64, $result)){
			$type = $result[2];
			$new_file = "./Uploads/headimg/".time().".{$type}";
			$new_filen = "/Uploads/headimg/".time().".{$type}";
			if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64)))){
				//echo '新文件保存成功：', $new_file;
				$where['id'] = intval($this->userinfo['id']);
				$data['headimgurl'] = $new_filen;
				$msg = D('address')->where($where)->save($data);
				if(!empty($msg)) {
					echo json_encode(1);
				} else {
					echo json_encode(2);
				}
			} else {
				echo json_encode(2);
			}
		}
	}

	public function editheadimg() {
		$info = D('address')->find($this->userinfo['id']);
		$this->assign('info',$info);
		$this->display();
	}

	public function zsc() {
		//echo "http://".$_SERVER['HTTP_HOST'];
		//U(MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME);
		//echo '<br>';
		//echo "http://".$_SERVER['HTTP_HOST'].__SELF__;
		//echo __SELF__;
		unset($_SESSION['info']);
		die();
		/*
		echo Pinyin(trim(str_replace(array(1,2,3,4,5,6,7,8,9,0),'','0001212ASD2323asd哈斯那个')));
		die();
		$signPackage = $this->signPackage;
		$signPackage['title'] = "苗木通讯录";
		$signPackage['link'] = 'http://'.C('DOMAIN').U('Index/index');
		$signPackage['desc'] = "众里寻他千百度，苗友尽在通讯录，苗商驰骋天地间，买卖沟通更方便。";
		$signPackage['imgUrl'] = 'http://zhumeng777.com/Uploads/2016-09-18/57de84aca6a35.jpg';
		$this->assign('signPackage',$signPackage);
		$this->display();
		*/
		//$this->display();
		Header("Content-type: image/PNG");/*告诉IE浏览器你做的程序是张图片*/
		$image = @imagecreatefrompng ("banner.png"); 
		imagepng ($image,null,0); /*压缩等级0-9，压缩后9最小，1最大*/
		imagedestroy ($image);
	}

	/**
	 * 加入通许录
	 */
	public function buy() {
		$manay = 30;
		$this->assign('manay',$manay);
		$lj = 2;
		$this->assign('lj',$lj);
		$this->assign('userid',$this->userinfo['id']);
		/*
		//$year = date('Y');
		$year = 2016;
		$string = $year."0101";
		$yeartime = strtotime($string);

		$string = ($year+2)."0101";
		$nextyeartime = strtotime($string);
		*/
		/*
		$time = time();
	    $pretime = $time-(60*60*24*365);
		*/

		//$info = M('address')->find($this->userinfo['id']);
		//if($info['paytime'] >= $yeartime and $info['paytime'] < $nextyeartime) {
		/*
		if($info['paytime'] > $pretime) {
			header('Location:'.U('Index/myself'));
			die();
		}
		*/

		$this->display();
	}

	public function buy2() {
		//$baseUrl = urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING']);
		//echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING'];
		//echo '<br/>';
		//echo $_SERVER['HTTP_HOST'];
		//die();
		$manay = 30;
		$this->assign('manay',$manay);

		$lj = 2;
		$this->assign('lj',$lj);

		$this->assign('userid',$this->userinfo['id']);

		$info = M('address')->find($this->userinfo['id']);
		//if($info['paytime'] >= $yeartime and $info['paytime'] < $nextyeartime) {
			//header('Location:'.U('Index/myself'));
			//die();
		//}
		$this->display();
	}

	/**
	 * 判断邀请码是否正确
	 */
	public function code() {
		$code = I('post.code','','strip_tags');
		if(!empty($code)) {
			$where['ecode'] = $code;
			$info = M('code')->where($where)->find();
			if(!empty($info)) {
				echo 1;
			} else {
				echo 2;
			}
		} else {
			echo 2;
		}
	}


	/**
	 * 修改个人信息
	 */
	public function editlist() {
		$info = D('address')->find($this->userinfo['id']);
		$this->assign('info',$info);
		$this->display();
	}

	/**
	 * 修改头像
	 */	
	public function edithead() {
		$info = D('address')->find($this->userinfo['id']);
		$this->assign('info',$info);
		$this->display();
	}

	/**
	 *
	 */
	public function ajaxhead() {
			$where['id'] = intval($this->userinfo['id']);
			$upload = new \Think\Upload();// 实例化上传类
			$upload->maxSize   =     1145728;// 设置附件上传大小
			$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
			$upload->rootPath  =      './Uploads/headimg/'; // 设置附件上传根目录
			// 上传单个文件 
			$info   =   $upload->uploadOne($_FILES['fileimg']);
			if(!$info) {// 上传错误提示错误信息
				//jssuccess($upload->getError(),U('Index/edithead'));
				echo json_encode(array(
					'code' => 2,
					'msg' => $upload->getError(),
					'file' => '',
				));
				die();
			}else{// 上传成功 获取上传文件信息
				$data['headimgurl'] = '/Uploads/headimg/'.$info['savepath'].$info['savename'];
			}
			$msg = D('address')->where($where)->save($data);
			if($msg) {
				//jssuccess('操作成功！',U('Index/editlist'));
				echo json_encode(array(
					'code' => 1,
					'msg' => '上传成功！',
					'file' => $data['headimgurl'],
				));
			} else {
				//jserror('操作失败！',U('Index/editlist'));
				echo json_encode(array(
					'code' => 2,
					'msg' => '上传失败！',
					'file' => '',
				));
			}
	}
	
	/**
	 * 个人中心修改列表
	 */	
	public function editbase() {
		if(IS_POST) {
			$_POST['id'] = intval($this->userinfo['id']);
			$msg = D('Admin/address')->msave();
			if($msg) {
				jssuccess('操作成功！',U('Index/editlist'));
			} else {
				jserror('操作失败！',U('Index/editlist'));
			}
			die();
		}
		$info = D('address')->find($this->userinfo['id']);
		if(!empty($info['mobile'])) {
			$mobilearr = json_decode($info['mobile'],true);
			$info['mobilearr'] = $mobilearr;
			$this->assign('mobilearr',$mobilearr);
		}
		$this->assign('info',$info);
		/**jssdk***/
		$signPackage = $this->signPackage;
		$signPackage['title'] = "苗木通讯录";
		$signPackage['link'] = 'http://'.C('DOMAIN').U('Index/index');
		$signPackage['desc'] = "众里寻他千百度，苗友尽在通讯录，苗商驰骋天地间，买卖沟通更方便。";
		$signPackage['imgUrl'] = 'http://zhumeng777.com/Uploads/2016-09-18/57de84aca6a35.jpg';
		$this->assign('signPackage',$signPackage);
		/****/
		$this->display();
	}
	
	/**
	 * 修改个人出售信息
	 */
	public function editcs() {
		if(IS_POST) {
			$_POST['id'] = intval($this->userinfo['id']);
			$msg = D('Admin/address')->msave();
			if($msg) {
				jssuccess('操作成功！',U('Index/editlist'));
			} else {
				jserror('操作失败！',U('Index/editlist'));
			}
			die();
		}
		$info = D('address')->find($this->userinfo['id']);
		$this->assign('info',$info);
		/**jssdk***/
		$signPackage = $this->signPackage;
		$signPackage['title'] = "苗木通讯录";
		$signPackage['link'] = 'http://'.C('DOMAIN').U('Index/index');
		$signPackage['desc'] = "众里寻他千百度，苗友尽在通讯录，苗商驰骋天地间，买卖沟通更方便。";
		$signPackage['imgUrl'] = 'http://zhumeng777.com/Uploads/2016-09-18/57de84aca6a35.jpg';
		$this->assign('signPackage',$signPackage);
		/****/
		$this->display();
	}
	
	/**
	 * 修改个人求购信息
	 */
	public function editqg() {
		if(IS_POST) {
			$_POST['id'] = intval($this->userinfo['id']);
			$msg = D('Admin/address')->msave();
			if($msg) {
				jssuccess('操作成功！',U('Index/editlist'));
			} else {
				jserror('操作失败！',U('Index/editlist'));
			}
			die();
		}
		$info = D('address')->find($this->userinfo['id']);
		$this->assign('info',$info);
		/**jssdk***/
		$signPackage = $this->signPackage;
		$signPackage['title'] = "苗木通讯录";
		$signPackage['link'] = 'http://'.C('DOMAIN').U('Index/index');
		$signPackage['desc'] = "众里寻他千百度，苗友尽在通讯录，苗商驰骋天地间，买卖沟通更方便。";
		$signPackage['imgUrl'] = 'http://zhumeng777.com/Uploads/2016-09-18/57de84aca6a35.jpg';
		$this->assign('signPackage',$signPackage);
		/****/
		$this->display();
	}
	
	/**
	 * 图片展示
	 */
	public function showimg() {
		$showimg = urldecode($_GET['img']);
		$this->assign('showimg',$showimg);
		$this->display();
	}

	public function photobak() {
		$photo = array_filter(explode('|',$this->userinfo['photo']));
		$this->assign('photo',$photo);
		$this->display();
	}

	public function photo() {
		if(!empty($this->userinfo['photo'])) {
			$photo = array_filter(explode('|',$this->userinfo['photo']));
			$this->assign('photo',$photo);
		}
		if(!empty($this->userinfo['desc'])) {
			$descarr = json_decode($this->userinfo['desc'],true);
			$this->assign('descarr',$descarr);
		}
		$this->display();
	}

	public function ajaxphoto() {
			if(!empty($this->userinfo['photo'])) {
				$photo = array_filter(explode('|',$this->userinfo['photo']));
			}
			if(count($photo) >= 8) {
				echo json_encode(array(
					'code' => 2,
					'msg' => '最多允许上传5张图片！',
					'file' => '',
				));
				die();
			}

			$where['id'] = intval($this->userinfo['id']);
			$upload = new \Think\Upload();// 实例化上传类
			$upload->maxSize   =     10485760;// 设置附件上传大小
			$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
			$upload->rootPath  =      './Uploads/'; // 设置附件上传根目录
			// 上传单个文件 
			$info   =   $upload->uploadOne($_FILES['fileimg']);
			if(!$info) {// 上传错误提示错误信息
				//jssuccess($upload->getError(),U('Index/edithead'));
				echo json_encode(array(
					'code' => 2,
					'msg' => $upload->getError(),
					'file' => '',
				));
				die();
			}else{// 上传成功 获取上传文件信息
				$photos = './Uploads/'.$info['savepath'].$info['savename'];

				$namearr = explode(".",$info['savename']);
				$namearr[0] = $namearr[0]."_".time();
				$names = implode(".",$namearr);

				$image = new \Think\Image(); 
				$image->open($photos);
				// 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
				$image->thumb(650, 650)->save('./Uploads/'.$info['savepath'].$names);
//$json = json_encode($info);
//file_put_contents('test.zsc',$json);

				//$photos = '/Uploads/'.$info['savepath'].$info['savename'];
				unlink('./Uploads/'.$info['savepath'].$info['savename']);
				$photos = '/Uploads/'.$info['savepath'].$names;
				$photo[] = $photos;
				$data['photo'] = implode("|",$photo);
			}
			$count = count($photo);
			$counts = $count-1;
			$msg = D('address')->where($where)->save($data);
			if($msg) {
				//jssuccess('操作成功！',U('Index/editlist'));
				echo json_encode(array(
					'code' => 1,
					'msg' => '上传成功！',
					'file' => $photos,
					//'num' => $counts,
				));
			} else {
				//jserror('操作失败！',U('Index/editlist'));
				echo json_encode(array(
					'code' => 2,
					'msg' => '上传失败！',
					'file' => '',
					//'num' => $counts,
				));
			}
	}
	
	/**
	 * 删除商品展示
	 */
	public function ajaxremoveimg() {
		$key = I('post.key','','intval');
		$filename = I('post.filename','','strip_tags');
		$where['id'] = intval($this->userinfo['id']);
		if(!empty($this->userinfo['photo'])) {
			$photo = array_filter(explode('|',$this->userinfo['photo']));
		}
		unlink(".".$photo[$key]);
		unset($photo[$key]);
		/****/
		if(!empty($this->userinfo['desc'])) {
			$descarr = json_decode($this->userinfo['desc'],true);
		}
		unset($descarr[$filename]);
		$data['desc'] = json_encode($descarr);
		/****/
		$data['photo'] = implode("|",$photo);
		$msg = D('address')->where($where)->save($data);
		if($msg) {
			echo json_encode(array(
				'code' => 1,
			));
		} else {
			echo json_encode(array(
				'code' => 2,
			));
		}
	}

	public function ajaxeditdesc() {
		$desc = I('post.desc','','strip_tags');
		$filename = I('post.filename','','strip_tags');

		$where['id'] = intval($this->userinfo['id']);
		if(!empty($this->userinfo['desc'])) {
			$descarr = json_decode($this->userinfo['desc'],true);
		}
		$descarr[$filename] = $desc;
		$data['desc'] = json_encode($descarr);
		$data['updatetime'] = time();
		$msg = D('address')->where($where)->save($data);
		if($msg) {
			echo json_encode(array(
				'code' => 1,
			));
		} else {
			echo json_encode(array(
				'code' => 2,
			));
		}
	}

	public function sel() {
		/*
		$where['ping'] = array('like','a%');
		$list = D('address')->field('id,nickname,ping')->where($where)->select();
		foreach($list as $key=>$value) {
			$data['nickname'] = delzm($value['nickname']);
			$pings = Pinyin($data['nickname']);
			if(empty($pings)) {
				$data['ping'] = 'h';
			} else {
				$data['ping'] = $pings;
			}
			$msg = D('address')->where(array('id'=>$value['id']))->save($data);
			if($msg) {
				echo $value['id'].' yes<br/>';
			} else {
				echo $value['id'].' no<br/>';
			}
		}
		die();
		echo '<pre>';
		print_r($list);
		die();
		//foreach() {
		//}
		//$name = 'aaaaaaaasd1221找那个';
		//$str = preg_replace('|[0-9a-zA-Z/]+|','',$name);
		*/
		//echo $_SERVER['HTTP_HOST'];
	}

}
